<template>
	<view>
		<view class="top-banner">
			还在担心被你的亲朋好友、同事亲戚看到而尴尬么？ 本平台支持相册隐私保护，若您选择相册公开，在相册公开上传处替换您的个人照片即可。<br />
			若您想将相册隐藏，在私密相册上传您的照片即可。<br />
			您上传的私密照片，只有在您的心动对象向您发送申请查看私密相册邀请并且经您本人同意后，对方才可查看到您的照片！
		</view>
		<view class="section">
			<text>公开相册</text>
			<imgUpload class="upload" :max="1" :action="`${uploadUrl}${state}/`" v-model="publicImage"
				@uploadSuccess="(res)=>uploadSuccess(res,'public')" @uploadFail="uploadFail"
				@imgDelete="(e)=>imgDelete(e,'public')" :headers="{'Authorization':token}">

			</imgUpload>
		</view>
		<view class="section">
			<text>私密相册</text>
			<imgUpload class="upload" :max="2" :action="`${uploadUrl}${state}/`" v-model="secretImage"
				@uploadSuccess="(res)=>uploadSuccess(res,'private')" @uploadFail="uploadFail"
				@imgDelete="(e)=>imgDelete(e,'private')" :headers="{'Authorization':token}">

			</imgUpload>
		</view>
		<view class="tip">
			您上传的照片必须是您本人的真实照片，能够展示您的仪容仪表，所有上传照片都会经过平台管理审核通过后显示。<br>请勿上传广告、违法、裸露、带有联系方式的照片，违法本平台公约操作有封号的风险。<br><br>  长相是天生的，但请尽量做到干净整洁，修边幅，好的衣品确实能够弥补80%的颜值缺陷，还请您用心选择进行展示的照片！！！
		</view>
	</view>
</template>

<script>
	import {
		url,
		request,
		login
	} from '@/service/api.js'
	import imgUpload from '@/components/htz-image-upload/htz-image-upload.vue'

	export default {
		data() {
			return {
				token: `Bearer ${uni.getStorageSync('auth')}`,
				state: 2,
				uploadUrl: url.upload,
				publicImage: [],
				secretImage: [],
				imgList: [],
			};
		},
		onLoad() {
			this.init()
		},
		components: {
			imgUpload: imgUpload
		},
		methods: {
			init() {
				this.publicImage = []
				this.secretImage = []
				request(url.getAlbum, 'GET', {}).then(res => {
					console.log(1111, res)
					this.imgList = res.data
					this.imgList.map(item => {
						if (item.type === 1) {
							this.publicImage.push(item.photo)
						} else {
							this.secretImage.push(item.photo)
						}
					})
					if (!this.publicImage.length) {
						this.publicImage = [require('./image/defaultImg.png')]
					}
				})
			},
			uploadSuccess(res, type) {
				let imglist = []
				let realUrl = ''
				if (type === 'public') {
					imglist = this.publicImage
					realUrl = url.uploadPublicImg
				} else {
					imglist = this.secretImage
					realUrl = url.uploadPrivateImg
				}
				if (res.statusCode === 200) {
					const result = JSON.parse(res.data)
					request(realUrl, 'get', {
						photo: result.data
					}).then(res => {
						if (res.status === 200) {
							uni.showToast({
								title: '上传成功',
							})
							this.init()
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						}
					}).catch(err => {
						debugger
						uni.showToast({
							title: err.msg,
							icon: 'error'
						})
					})
				}

			},
			uploadFail(e) {
				uni.showToast({
					title: '错误',
					text: '上传图片失败'
				})
			},
			imgDelete(e, type) {
				let selectedItem = this.imgList.find(item => item.photo === e.del)
				console.log(this.imgList.find(item => item.photo === e.del))
				if (selectedItem) {

				}
				debugger
				let imglist = []
				if (type === 'public') {
					imglist = this.publicImage
				} else {
					imglist = this.secretImage
				}
				request(url.deleteImg, 'GET', {
					photoId: selectedItem.id
				}).then(res => {
					if (res.status === 200) {
						uni.showToast({
							title: '删除成功',
						})
						this.init()
					} else {
						uni.showToast({
							title: '删除失败',
							icon: "none"
						})
					}
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: "none"
					})
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	* {
		box-sizing: border-box;
	}

	.top-banner {
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		background: linear-gradient(-41deg, rgba(49, 104, 174, 0.57), rgba(145, 83, 188, 0.57));
		font-size: 13px;
		font-family: SimHei;
		color: #FFFFFF;
		line-height: 19px;
		padding: 15px;
	}

	.section {
		margin-top: 10px;
		background-color: #fff;
		width: 100%;
		padding: 15px;
		font-size: 13px;
		font-family: SimHei;
		color: #888888;

		.upload {
			margin-top: 30px;
		}
	}

	.tip {
		color: #888888;
		padding: 15px;
		font-size: 13px;
		line-height: 1.5;
	}
</style>
